package DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import obj.Type;
import DAO.DAO;

public class TypeDAO extends DAO<Type> {

	public boolean create(Type obj) {
		try {
			PreparedStatement ps = this.connect
					.prepareStatement("INSERT INTO TYPE(T_LIB) VALUES('"+obj.getLib()+"')");
			ps.executeUpdate();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public Type find(String id) {
		Type typ = new Type();
		try {
			ResultSet rs = this.connect.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE).executeQuery(
					"SELECT * FROM TYPE WHERE T_LIB='" + id+"'");
			if (rs.first()) {
				typ = new Type(id);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return typ;
	}

	public boolean update(Type obj) {
		try {
			PreparedStatement ps = this.connect
					.prepareStatement("UPDATE TYPE SET T_LIB='"+obj.getLib()+"' WHERE T_LIB ='"+obj.getLib()+"'");
			ps.executeUpdate();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public void delete(Type obj) {
		try {
			PreparedStatement ps = this.connect
					.prepareStatement("DELETE FROM TYPE WHERE T_LIB ='"+obj.getLib()+"'");
			ps.executeUpdate();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public ArrayList<String> selectAll() {
		ArrayList<String> ltype= new ArrayList<String>();
		try {
			ResultSet rs = this.connect.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE).executeQuery(
					"SELECT * FROM TYPE");
			
			while(rs.next()){
				ltype.add(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ltype;
	}
}
